public class huffmanTree implements Comparable
{
    BinaryTree root; // root of tree
    int totalWeight; // weight of tree
    Node nodo;

    public huffmanTree(Node e)
    // post: construct a leaf with associated character
    {
        root = new BinaryTree(e);
        totalWeight = e.frequency;
        nodo = e;
    }

    public huffmanTree(huffmanTree left, huffmanTree right)
    // pre: left and right non-null
    // post: merge two trees together and merge their weights
    {
        this.totalWeight = left.totalWeight + right.totalWeight;
        root = new BinaryTree(null,left.root,right.root);
    }

    public int compareTo(Object other)
    // pre: other is non-null
    // post: return integer reflecting relation between values
    {
        huffmanTree that = (huffmanTree)other;
        return this.totalWeight - that.totalWeight;
    }

    public boolean equals(Object that)
    // post: return true if this and that are same tree instance
    {
        return this == that;
    }
    
    public void print()
    // post: print out strings associated with characters in tree
    {
        print(this.root,"");
    }

    protected void print(BinaryTree r, String representation)
    // post: print out strings associated with chars in tree r,
    //       prefixed by representation
    {
        if (!r.left().isEmpty())
        {   // interior node
            print(r.left(),representation+"0"); // append a 0
            print(r.right(),representation+"1"); // append a 1
        } 
        else 
        { // leaf; print encoding
            Node e = (Node)r.value();
            System.out.println("Encoding of "+e.ch+" is "+
               representation+" (frequency was "+e.frequency+")");
        }
    }
    
}


